//logs.js
import store from "../../utils/factory";
import {
    getYsDetail,
    getSchedule,
    getComment
} from "../../utils/config";
//获取应用实例
const app = getApp();
let that = null;

Page({
    data: {
        count: 0,
        isActive: 1,
        ysDetail: {},
        schedules: [],
        evaList: [],
        year: 0,
        month: 0,
        commentCount: 0,
        date: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
        dateArr: [],
        isToday: 0,
        isTodayWeek: false,
        todayIndex: 0
    },
    onLoad: function (e) {
        that = this;
        let now = new Date();
        let year = now.getFullYear();
        let month = now.getMonth() + 1;
        this.setData({
            year: year,
            month: month,
            isToday: '' + year + month + now.getDate()
        })
        console.log("today", this.data.isToday)
        this.getDetail(e.id);
        this.getSchedule(e.id);
        this.getComment(e.id)
    },
    onShow() {

    },
    onShareAppMessage() {
        let appData = this.data.ysDetail;
        return {
            title: `${appData.name}-月嫂详情`,
            imageUrl: `${appData.photo}`,
            path: `/pages/logs/logs?id=${appData.id}`
        }
    },
    /**
* 分享到朋友圈
* 前提是必须定义了：﻿onShareAppMessage，传参是在query中定义
* 这个方法中分享的地址就是当前页面地址，所以不需要指定path
*/
    onShareTimeline: function (res) {
        let appData = this.data.ysDetail;
        return {
            title: `${appData.name}-月嫂详情`,
            imageUrl: `${appData.photo}`,
            path: `/pages/logs/logs?id=${appData.id}`
        }
    },
    choose() { },
    dateInit: function (setYear, setMonth) {
        //全部时间的月份都是按0~11基准，显示月份才+1
        let dateArr = []; //需要遍历的日历数组数据
        let arrLen = 0; //dateArr的数组长度
        let now = setYear ? new Date(setYear, setMonth) : new Date();
        let year = setYear || now.getFullYear();
        let nextYear = 0;
        let month = setMonth || now.getMonth(); //没有+1方便后面计算当月总天数
        let nextMonth = (month) > 11 ? 1 : (month);
        let startWeek = new Date(year + '/' + (month) + '/' + 1).getDay(); //目标月1号对应的星期
        let dayNums = new Date(year, nextMonth, 0).getDate(); //获取目标月有多少天
        let obj = {};
        let num = 0;
        if (month + 1 > 11) {
            nextYear = year + 1;
            dayNums = new Date(nextYear, nextMonth, 0).getDate();
            console.log("dayNums2", dayNums)
        }
        arrLen = startWeek + dayNums;
        if (that.data.schedules.length > 0) {
            for (let i = 0; i < arrLen; i++) {
                if (i >= startWeek) {
                    num = i - startWeek + 1;
                    obj = {
                        isToday: '' + year + (month) + num,
                        dateNum: num,
                        weight: that.data.schedules[num - 1] == "1" ? "已预约" : "空闲"
                    }
                } else {
                    obj = {};
                }
                dateArr[i] = obj;
            }
        } else {
            for (let i = 0; i < arrLen; i++) {
                if (i >= startWeek) {
                    num = i - startWeek + 1;
                    obj = {
                        isToday: '' + year + (month) + num,
                        dateNum: num,
                        weight: "空闲"
                    }
                } else {
                    obj = {};
                }
                dateArr[i] = obj;
            }
        }
        this.setData({
            dateArr: dateArr
        })
        let nowDate = new Date();
        let nowYear = nowDate.getFullYear();
        let nowMonth = nowDate.getMonth() + 1;
        let nowWeek = nowDate.getDay();
        let getYear = setYear || nowYear;
        let getMonth = setMonth >= 0 ? (setMonth) : nowMonth;

        if (nowYear == getYear && nowMonth == getMonth) {
            this.setData({
                isTodayWeek: true,
                todayIndex: nowWeek
            })
        } else {
            this.setData({
                isTodayWeek: false,
                todayIndex: -1
            })
        }
    },
    lastMonth: function () {
        //全部时间的月份都是按0~11基准，显示月份才+1
        let year = this.data.month - 2 < 0 ? this.data.year - 1 : this.data.year;
        let month = this.data.month - 2 < 0 ? 11 : this.data.month - 2;
        this.setData({
            year: year,
            month: (month + 1)
        })
        this.getSchedule(that.data.ysDetail.id);
    },
    nextMonth: function () {
        //全部时间的月份都是按0~11基准，显示月份才+1
        let year = this.data.month > 11 ? this.data.year + 1 : this.data.year;
        let month = this.data.month > 11 ? 0 : this.data.month;
        this.setData({
            year: year,
            month: (month + 1)
        })
        this.getSchedule(that.data.ysDetail.id);
    },
    selectTab(e) {
        const index = e.currentTarget.dataset.index;
        console.log(index)
        this.setData({
            isActive: index
        })
    },
    getComment(id) {
        getComment({
            page: 1,
            limit: 50,
            id: id
        }).then(res => {
            that.setData({
                commentCount: res.count,
                evaList: res.data
            })
        })
    },
    getDetail(id) {
        wx.showLoading({
            title: "加载信息中..."
        });
        getYsDetail({
            id: id
        }).then(res => {
            res.data.star = parseInt(res.data.star);
            this.setData({
                ysDetail: res.data
            });
            wx.hideLoading();
        });
    },
    getSchedule(id) {
        let month = this.data.month < 10 ? `0${this.data.month}` : this.data.month;
        let time = `${this.data.year}${month}`
        getSchedule({
            id: id,
            time: time
        }).then(res => {
            if (res.data.schedules.length > 0) {
                that.setData({
                    schedules: res.data.schedules
                })
            } else {
                that.setData({
                    schedules: []
                })
            }
            that.dateInit(that.data.year, that.data.month)
        })
    },
    previewImage(e) {
        const url = e.currentTarget.dataset.url
        wx.previewImage({
            current: url, // 当前显示图片的http链接
            urls: [url] // 需要预览的图片http链接列表
        })
    },
    appointment() {
        if (!store.get("userInfo")) {
            wx.navigateTo({
                url: "../autoLogin/autoLogin" // 页面 B
            });
            return false;
        }
        store.set("ysDetail", this.data.ysDetail)
        wx.navigateTo({
            url: `../submitOrder/submitOrder`
        });
    },
});